package com.bright.ghj.overt.util;

import com.bright.ghj.common.pojo.query.PageQuery;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;

/**
 * @Author hxj
 * @Date 2024/2/1 11:20
 * @Description
 */
public class EntityManagerUtil {

    public static Long queryForCount(EntityManager entityManager, String querySQL, Map<String, Object> params) {
        Query countNativeQuery = getNativeQuery(entityManager, querySQL, params);
        return Long.valueOf(countNativeQuery.getSingleResult().toString());
    }

    public static List<Map<String, Object>> queryForMapList(EntityManager entityManager, String querySQL, Map<String, Object> params, PageQuery pageQuery) {
        Query nativeQuery = getNativeQuery(entityManager, querySQL, params);
        nativeQuery.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        // 实现分页
        nativeQuery.setFirstResult((pageQuery.getPageNumber()-1) * pageQuery.getPageSize());
        nativeQuery.setMaxResults(pageQuery.getPageSize());
        return nativeQuery.getResultList();
    }

    // 不分页
    public static List<Map<String, Object>> queryForMapList(EntityManager entityManager, String querySQL, Map<String, Object> params) {
        Query nativeQuery = getNativeQuery(entityManager, querySQL, params);
        nativeQuery.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        return nativeQuery.getResultList();
    }

    public static <T> List<T> queryForClass(EntityManager entityManager, String querySQL, Map<String, Object> params, Class<T> clazz, PageQuery pageQuery) {
        Query nativeQuery = getNativeQuery(entityManager, querySQL, params);
        nativeQuery.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(clazz));
        // 实现分页
        nativeQuery.setFirstResult((pageQuery.getPageNumber()-1) * pageQuery.getPageSize());
        nativeQuery.setMaxResults(pageQuery.getPageSize());
        return nativeQuery.getResultList();
    }

    // 不分页
    public static <T> List<T> queryForClass(EntityManager entityManager, String querySQL, Map<String, Object> params, Class<T> clazz) {
        Query nativeQuery = getNativeQuery(entityManager, querySQL, params);
        nativeQuery.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(clazz));
        return nativeQuery.getResultList();
    }

    // 更新
    public static void update(EntityManager entityManager, String updateSQL, Map<String, Object> params) {
        Query nativeQuery = getNativeQuery(entityManager, updateSQL, params);
        nativeQuery.executeUpdate();
    }

    private static Query getNativeQuery(EntityManager entityManager, String querySql, Map<String, Object> params) {
        Query nativeQuery = entityManager.createNativeQuery(querySql);
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            nativeQuery.setParameter(entry.getKey(), entry.getValue());
        }
        return nativeQuery;
    }



}
